package com.ktjy.web.servlet;

import com.alibaba.fastjson.JSONArray;
import com.ktjy.web.entity.AjaxResult;
import com.ktjy.web.entity.CategoryItem;
import com.ktjy.web.service.CategoryItemService;
import com.ktjy.web.service.impl.CategoryItemServiceImpl;
import com.ktjy.web.util.BaseSevlet;
import my.framework.anno.RequestMapping;


import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

//@WebServlet(name = "CategoryItemsServlet", urlPatterns = "/api/category/mutli")
public class CategoryItemServlet extends BaseSevlet {
    CategoryItemService service = new CategoryItemServiceImpl();


    @RequestMapping("/CategoryItem")
    public Object CategoryItem(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //1、创建out对象
        PrintWriter out = response.getWriter();
        // 2、获取opr请求参数
        String opr = request.getParameter("opr");
        AjaxResult result = null;
        if ("list".equals(opr)) {
            // 3、调用service中的方法
            List<CategoryItem> list = service.getOneLevelCategory();
            result = AjaxResult.success("前台分类列表加载成功!", list);
        } else if ("findAll".equals(opr)) {
            List<CategoryItem> list = service.getCategoryAndGoods();
            result = AjaxResult.success("一级分类列表加载成功!", list);
        } else if ("level".equals(opr)) {
            List<CategoryItem> list=service.selByLevel();
            result = AjaxResult.success("二级分类列表加载成功!", list);
        }
        // 将result对象转换成JSON字符串再响应到小程序端
        return result;
    }
}
